# Fix tenantId variables in PermissionServiceImpl
$file = "kexio-user-management/src/main/java/com/kexio/user/service/impl/PermissionServiceImpl.java"
$content = Get-Content $file -Raw

# Replace all catch blocks that use tenantId without defining it
$content = $content -replace '(\s*} catch \(Exception e\) \{)\s*\n\s*(logger\.(error|warn|info|debug)\([^}]*tenantId[^}]*\}[^}]*tenantId[^)]*\))', "`$1`n            String tenantId = SecurityContext.getCurrentTenantId();`n            `$2"

# Replace method bodies that use tenantId without defining it (simple case)
$content = $content -replace '(\s*try \{)\s*\n(\s*)((?!.*String tenantId).*)\n((?:[^}]|[^{]*\{[^}]*\})*tenantId)', "`$1`n`$2    String tenantId = SecurityContext.getCurrentTenantId();`n`$2`$3`n`$4"

# For methods that start with logging and use tenantId
$content = $content -replace '(\s*@Override\s*\n\s*public[^{]*\{)\s*\n(\s*)(logger\.(error|warn|info|debug)\([^}]*tenantId)', "`$1`n`$2    String tenantId = SecurityContext.getCurrentTenantId();`n`$2`$3"

Set-Content $file $content
Write-Host "Fixed tenantId issues in PermissionServiceImpl"

